package com.cm367.teamc.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import com.cm367.teamc.LeaveRequest;
import com.cm367.teamc.bean.Leave;
import com.cm367.teamc.dao.LeaveDAO;
import com.cm367.teamc.dao.LeaveRulesDAO;
import com.cm367.teamc.dao.PublicHolidayDAO;
import com.cm367.teamc.util.DateUtil;

@ManagedBean
@ApplicationScoped
public class LeaveService {

	@ManagedProperty(value = "#{leaveDAO}")
	private LeaveDAO leaveDAO;

	@ManagedProperty(value = "#{leaveRulesDAO}")
	private LeaveRulesDAO leaveRulesDAO;

	@ManagedProperty(value = "#{publicHolidayDAO}")
	private PublicHolidayDAO publicHolidayDAO;

	public LeaveDAO getLeaveDAO() {
		return leaveDAO;
	}

	public void setLeaveDAO(LeaveDAO leaveDAO) {
		this.leaveDAO = leaveDAO;
	}

	public LeaveRulesDAO getLeaveRulesDAO() {
		return leaveRulesDAO;
	}

	public void setLeaveRulesDAO(LeaveRulesDAO leaveRulesDAO) {
		this.leaveRulesDAO = leaveRulesDAO;
	}

	public PublicHolidayDAO getPublicHolidayDAO() {
		return publicHolidayDAO;
	}

	public void setPublicHolidayDAO(PublicHolidayDAO publicHolidayDAO) {
		this.publicHolidayDAO = publicHolidayDAO;
	}

	public List<LeaveRequest> getLeaveHistory(String userid, String type) {
		List<LeaveRequest> list = new ArrayList<LeaveRequest>();
		try {
			list = leaveDAO.getLeaveRequest(userid, type);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<LeaveRequest> getAllLeaveHistory(String userid, String type) {
		List<LeaveRequest> list = new ArrayList<LeaveRequest>();
		try {
			list = leaveDAO.getAllLeaveRequest(userid, type);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Leave> getLeave(String userid, String type, String dateFrom,
			String dateTo) {
		List<Leave> list = new ArrayList<Leave>();
		try {
			list = leaveDAO.getLeave(userid, type, dateFrom, dateTo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<Leave> getAllLeave(String userid, String type, String dateFrom,
			String dateTo) {
		List<Leave> list = new ArrayList<Leave>();
		try {
			list = leaveDAO.getAllLeave(userid, type, dateFrom, dateTo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public void changeStatus(String status, int leaveID) {
		try {
			leaveDAO.changeStatus(status, leaveID);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	// Used for manager
	public List<Leave> getUsersLeave(String dept) {
		List<Leave> list = new ArrayList<Leave>();
		try {
			list = leaveDAO.getUsersLeave(dept);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public String createLeave(String userID, String dateFrom, String dateTo,
			int days, String reason, String type) {
		String result = null;
		try {
			boolean valid = leaveDAO.isLeaveValid(userID, dateFrom, dateTo);
			if (!valid) {
				result = "1";
				return result;
			}
			int yearA = DateUtil.getYear(dateFrom);
			int yearB = DateUtil.getYear(dateTo);
			if (yearA == yearB) {
				int left = leaveRulesDAO.getLeft(userID, type, yearA);
				if (days > left) {
					result = "2";
					return result;
				}
			} else {
				List<String> holidays = publicHolidayDAO
						.findHolidayDates(yearA);
				int dayA = DateUtil
						.countDays(yearA, dateFrom, dateTo, holidays);
				int leftA = leaveRulesDAO.getLeft(userID, type, yearA);
				if (dayA > leftA) {
					result = "2";
					return result;
				}
				holidays = publicHolidayDAO.findHolidayDates(yearB);
				int dayB = DateUtil
						.countDays(yearB, dateFrom, dateTo, holidays);
				int leftB = leaveRulesDAO.getLeft(userID, type, yearB);
				if (dayB > leftB) {
					result = "2";
					return result;
				}
			}
			leaveDAO.createLeave(userID, dateFrom, dateTo, days, reason, type);
			result = "3";
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
